﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spikes : MonoBehaviour {

    private Transform m_transform;
    private Transform son_transform;

    private Vector3 normalPos;
    private Vector3 tarfetPos;

	void Start () {
        m_transform = GetComponent<Transform>();
        son_transform = m_transform.Find("moving_spikes_b");

        normalPos = son_transform.position;
        tarfetPos = m_transform.position + Vector3.up * 0.15f;

        StartCoroutine("UpAndDown");
    }	

    private IEnumerator UpAndDown()
    {
        while (true)
        {
            StopCoroutine("Down");
            StartCoroutine("Up");
            yield return new WaitForSeconds(2.0f);
            StopCoroutine("Up");
            StartCoroutine("Down");
            yield return new WaitForSeconds(2.0f);
        }
    }

    private IEnumerator Up()
    {
        while(true){
            son_transform.position = Vector3.Lerp(son_transform.position, tarfetPos, Time.deltaTime * 10);
            yield return null;
        }
    }

    private IEnumerator Down()
    {
        while (true)
        {
            son_transform.position = Vector3.Lerp(son_transform.position, normalPos, Time.deltaTime * 10);
            yield return null;
        }
    }
}
